﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Collections.Generic;



    public partial class back_ViewMenu : System.Web.UI.Page
    {
        #region Variable Declaration
        IDataRepository _dataObject = new DataRepository();
        Menus objMenu;
        List<Menus> objlstMenu = new List<Menus>();
        #endregion Menu
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindMenuGrid();
            }
        }

        private void bindMenuGrid()
        {
            objMenu = new Menus(_dataObject);
            objlstMenu = objMenu.GetAllMenus();

            if (objlstMenu.Count > 0)
            {
                grdMenus.Visible = true;
                grdMenus.DataSource = objlstMenu;
                grdMenus.DataBind();
            }
            else
            {
                grdMenus.Visible = false;
                divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
                divRed.Attributes.Add("class", "alert alert-error");
            }
        }
        protected void grdMenus_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {
                System.Web.UI.WebControls.Label hdnMenuId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnMenuId");
                System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnEdit");
                btnEdit.PostBackUrl = "AddMenu.aspx?id=" + hdnMenuId.Text;
            }
        }
        protected void grdMenus_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString().ToLower() == "editmenu")
            {
                System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
                System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
                Response.Redirect(btnEdit.PostBackUrl);
            }

            if (e.CommandName.ToString().ToLower().Trim().Equals("deletemenu"))
            {
                System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
                System.Web.UI.WebControls.Label hdnMenuId = (System.Web.UI.WebControls.Label)row.FindControl("hdnMenuId");
                objMenu = new Menus(_dataObject);
                objMenu.Id = Convert.ToInt32(hdnMenuId.Text);
                objMenu.DeleteMenu();
                bindMenuGrid();
                divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
                divRed.Attributes.Add("class", "alert alert-error");
            }
        }
        protected void grdMenus_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {         
            grdMenus.PageIndex = e.NewPageIndex;
            bindMenuGrid();
        }
        
    }
